//
//  LoginViewController.m
//  WuXi_DevelopStatisticMonitor
//
//  Created by Vergil on 2017/4/21.
//  Copyright © 2017年 Vergil. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *userNameView;//账号view
@property (weak, nonatomic) IBOutlet UIView *passWordView;//密码view
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;//账号输入框
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;//密码输入框
@property (weak, nonatomic) IBOutlet UIImageView *rememberPWImage;//记住密码图片
@property (weak, nonatomic) IBOutlet UIImageView *autoLoginImage;//自动登录图片
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;//登录按钮

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置UI
    _userNameView.layer.cornerRadius = 5;
    _userNameView.layer.borderWidth = 0.4;
    _passWordView.layer.cornerRadius = 5;
    _passWordView.layer.borderWidth = 0.4;
    _loginBtn.layer.cornerRadius = 5;
    
    _userNameTF.text = [AppSetting shareAppSetting].userName;
    
    if ([AppSetting shareAppSetting].rememberPassword) {
        //FIXME: 密码图片
        _rememberPWImage.image = [UIImage imageNamed:@""];
        _passWordTF.text = [[AppSetting shareAppSetting] passwordForUserName:_userNameTF.text];
    } else {
        _rememberPWImage.image = [UIImage imageNamed:@""];
    }
    
    if ([AppSetting shareAppSetting].autoLogin) {
        [self loginClick:nil];
        //FIXME: 自动登录图片
        _autoLoginImage.image = [UIImage imageNamed:@""];
    } else {
        _autoLoginImage.image = [UIImage imageNamed:@""];
    }
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

//MARK: - 点击记住密码
- (IBAction)rememberPWClick:(id)sender {
    
    [AppSetting shareAppSetting].rememberPassword = ![AppSetting shareAppSetting].rememberPassword;
    if ([AppSetting shareAppSetting].rememberPassword) {
        //FIXME: 密码图片
        _rememberPWImage.image = [UIImage imageNamed:@""];
    } else {
        _rememberPWImage.image = [UIImage imageNamed:@""];
    }
    
}

//MARK: - 点击自动登录
- (IBAction)autoLoginClick:(id)sender {
    
    [AppSetting shareAppSetting].autoLogin = ![AppSetting shareAppSetting].autoLogin;
    if ([AppSetting shareAppSetting].autoLogin) {
        //FIXME: 自动登录图片
        _autoLoginImage.image = [UIImage imageNamed:@""];
    } else {
        _autoLoginImage.image = [UIImage imageNamed:@""];
    }
    
}

//MARK: - 点击登录按钮
- (IBAction)loginClick:(id)sender {
    
    if (![self loginTextJudge]) {
        return;
    }
    
    NSDictionary *dic = @{};
    [NetworkRequest loginRequestParameter:dic Success:^(NSDictionary *response) {
        
    } Error:^(NSError *error) {
        
    }];
    
    
}

//判断是否已填
- (BOOL)loginTextJudge {
    
    if (_userNameTF.text.length == 0) {
        [self showTextHUDWithMessage:@"请先输入账号"];
        return NO;
    } else if (_passWordTF.text.length == 0) {
        [self showTextHUDWithMessage:@"请先输入密码"];
        return NO;
    }
    return nil;
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == _userNameTF) {
        [_passWordTF becomeFirstResponder];
    } else {
        [self loginClick:nil];
    }
    return YES;
    
}


@end
